package cn.itcast.hotel;

import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.IOException;

import static cn.itcast.hotel.constants.HotelContstants.HOTEL_MAPPING_TEMPLATE;

/**
 * @author guozidi  2023-7-28
 * @Description
 * @package cn.itcast.hotel
 */
public class HotelESTestDemo {


    RestHighLevelClient client = null;

    @BeforeEach
    void before() {
        // 0.初始化Client对象
        client = new RestHighLevelClient(RestClient.builder(
                        HttpHost.create("http://192.168.200.100:9200"))
                );
    }

    @AfterEach
    void after() throws IOException {
        // .关闭client
        client.close();
    }



    /**
     * 创建索引库
     */
    @Test
    void testCreateHotelIndex() throws IOException {
        // 1.创建 创建索引库的请求对象
        CreateIndexRequest request = new CreateIndexRequest("hotel");
        // 2.设定数据
        request.source(HOTEL_MAPPING_TEMPLATE, XContentType.JSON);
        // 3.提交请求
        client.indices().create(request, RequestOptions.DEFAULT);
    }

    /*
    删除索引库
     */
    @Test
    void testDeleteHotelIndex() throws IOException {

        // 2.创建请求对象
        DeleteIndexRequest request = new DeleteIndexRequest("hotel");
        // 1.发送请求
        client.indices().delete(request,RequestOptions.DEFAULT);

    }

    /*
    判断索引库是否存在
     */
    @Test
    void testExistsHotelIndex() throws IOException {

        // 2.创建请求对象
        GetIndexRequest request = new GetIndexRequest("hotel");
        // 1.发起请求
        boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
        System.out.println(exists);
    }


}
